//
//  UIViewController+Lc.m
//  18Birdies
//
//  Created by Lc on 15/11/18.
//  Copyright © 2015年 Lc. All rights reserved.
//

#import "UIViewController+Lc.h"
#import "UIView+Lc.h"
#import "BaseViewController.h"
#import "LcUIUtility.h"

@implementation UIViewController (Lc)

+ (UIViewController*)currentViewControllerWithRoot:(UIViewController*)rootViewController {
  UIViewController* viewController = rootViewController;
  while (true) {
    if (viewController.presentedViewController != nil) {
      viewController = viewController.presentedViewController;
    } else if ([viewController isKindOfClass:UINavigationController.class]) {
      viewController = [(UINavigationController*)viewController visibleViewController];
    } else if ([viewController isKindOfClass:UITabBarController.class]) {
      viewController = [(UITabBarController*)viewController selectedViewController];
    } else {
      return viewController;
    }
  }
}

+ (UIViewController*)currentViewControllerInWindow:(UIWindow*)window {
  UIViewController* rootViewController = window.rootViewController;
  return [self currentViewControllerWithRoot:rootViewController];
}

+ (UIViewController*)currentViewController {
  UIViewController* rootViewController = nil;
  for (UIWindow* window in [UIApplication sharedApplication].windows) {
    if (window.rootViewController != nil) {
      rootViewController = window.rootViewController;
      break;
    }
  }
  return [self currentViewControllerWithRoot:rootViewController];
}

- (BOOL)isInCurrentViewController {
  UIViewController* currentViewController = [UIViewController currentViewController];
  if (self == currentViewController || [currentViewController containsController:self]) {
    return YES;
  }
  return NO;
}

- (UIViewController*)currentViewController {
  return [UIViewController currentViewController];
}

- (BOOL)isCurrentViewController {
  return self == [self currentViewController];
}

- (BOOL)isNavigaionController {
  return [self isKindOfClass:[UINavigationController class]];
}

- (BOOL)isTabBarController {
  return [self isKindOfClass:[UITabBarController class]];
}

- (CGFloat)width {
  return self.view.width;
}

- (CGFloat)height {
  return self.view.height;
}

- (void)presentViewController:(UIViewController*)viewControllerToPresent
             usePushAnimation:(BOOL)usePushAnimation
                   completion:(void (^)(void))completion {
  if (usePushAnimation) {
    CATransition* transition = [CATransition animation];
    transition.duration = SYSTEM_TRANSITION_DURATION;
    transition.timingFunction =
        [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromRight;
    [self.view.window.layer addAnimation:transition forKey:nil];
  }
  [self presentViewController:viewControllerToPresent animated:NO completion:completion];
}

- (void)presentViewController:(UIViewController*)viewControllerToPresent
            useAlphaAnimation:(BOOL)useAlphaAnimation
                   completion:(void (^)(void))completion {
  if (useAlphaAnimation) {
    viewControllerToPresent.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
  }
  [self presentViewController:viewControllerToPresent animated:YES completion:completion];
}

- (void)presentTransparentViewController:(UIViewController*)viewControllerToPresent
                                animated:(BOOL)animated
                              completion:(void (^)(void))completion {
  viewControllerToPresent.modalPresentationStyle = UIModalPresentationOverFullScreen;
  viewControllerToPresent.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
  [self presentViewController:viewControllerToPresent animated:animated completion:completion];
}

- (void)dismissViewControllerWithPopAnimation:(BOOL)flag completion:(void (^)(void))completion {
  if (flag) {
    CATransition* transition = [CATransition animation];
    transition.duration = SYSTEM_TRANSITION_DURATION;
    transition.timingFunction =
        [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromLeft;
    [self.view.window.layer addAnimation:transition forKey:nil];
  }
  [self dismissViewControllerAnimated:NO completion:completion];
}

#pragma mark - addChildViewController

- (void)showFillSizeController:(UIViewController*)controller {
  [self showController:controller inView:self.view updateViewBlock:^{
    controller.view.autoresizingMask =
    UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    controller.view.frame = self.view.bounds;
  }];
}

- (void)showFillSizeController:(UIViewController*)controller inView:(UIView*)containerView {
  if (controller != nil && containerView != nil) {

    [self showController:controller inView:containerView updateViewBlock:^{
      controller.view.autoresizingMask =
      UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
      controller.view.frame = containerView.bounds;
    }];
  }
}

- (void)showController:(UIViewController*)controller {
  [self showController:controller inView:self.view updateViewBlock:nil];
}

- (void)showController:(UIViewController*)controller inView:(UIView*)containerView {
  [self showController:controller inView:containerView updateViewBlock:nil];
}

- (void)showController:(UIViewController*)controller inView:(UIView*)containerView updateViewBlock:(void (^)(void))updateViewBlock {
  if (controller != nil && ![self containsController:controller]) {
    // addChildViewController will call [child willMoveToParentViewController:self] method
    [self addChildViewController:controller];
    [containerView addSubview:controller.view];
    if (updateViewBlock != nil) {
      updateViewBlock();
    }
    [controller didMoveToParentViewController:self];
  }
}

- (void)hideController:(UIViewController*)controller {
  if (controller != nil && [self containsController:controller]) {
    [controller willMoveToParentViewController:nil];
    [controller.view removeFromSuperview];
    [controller removeFromParentViewController];
  }
}

- (BOOL)containsController:(UIViewController*)controller {
  if (controller != nil) {
    return [[self childViewControllers] containsObject:controller];
  }
  return NO;
}

- (NSString*)hierarchyContext {
  if ([self isKindOfClass:BaseViewController.class]) {
    return [self hierarchyContext];
  } else {
    return NSStringFromClass(self.class);
  }
}

@end
